<template>
  <div style="margin-top:20px">
    <!-- ----------------------------------S7_300------------------------------------------->
    <el-table border ref="multipleTable" height="520" :data="rule_map" size='mini'>
      <template slot="empty">
        <el-empty :description="$t('暂无数据')"></el-empty>
      </template>
      <el-table-column prop="rule_id" align='center' :label="$t('规则ID')" width="80" />
      <el-table-column prop="proto" align='center' :formatter="protoValue" width="120" :label="$t('协议名称')" />
      <el-table-column prop="action" align='center' :formatter="matchOptionFormatter" width="80" :label="$t('动作')" />
      <el-table-column prop="access_mode" align='center' width="140" :label="$t('访问类型')">
        <template slot-scope="scope">
          {{ $t(accessModeList[scope.row.access_mode]) }}
        </template>
      </el-table-column>
      <el-table-column prop="func" align='center' width="140" :label="$t('读写属性')">
        <template slot-scope="scope">
          {{ $t(s7PlusFuncList[scope.row.func]) || parseInt(scope.row.func) }}
        </template>
      </el-table-column>
      <el-table-column prop="register_interval" align='center' width="140" :label="$t('寄存器区间')">
        <template slot-scope="scope">
          {{ $t(s7PlusIntervalList[scope.row.register_interval]) }}
        </template>
      </el-table-column>
      <el-table-column prop="register_address" align='center' :formatter="registerAddressFormatter" width="140" :label="$t('寄存器地址范围')" />
      <el-table-column align="center" :label="$t('操作')" width="160" fixed='right'>
        <template slot-scope="scope">
          <el-button type="success" plain @click="editRule(scope.row)" size='mini' :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
          <el-button type="danger" plain @click="deleteRule(scope.row)" size='mini' :class="'del' +(scope.$index+1)">{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import ruleArray from '../../ruleArray'
export default {
  name: 'S7300PlusTable',
  props: {
    ruleMap: {
      type: Array,
      default () {
        return []
      }
    }
  },
  watch: {
    ruleMap: {
      handler (newValue, oldValue) {
        this.rule_map = []
        newValue.forEach(val => {
          if (val.proto === 's7_300_plus') {
            this.rule_map.push(val)
          }
        })
      },
      deep: true // 深度监听
    }
  },
  data () {
    return {
      rule: ruleArray.ruleShow,
      rule_map: [],
      s7PlusFuncList: ruleArray.s7PlusFuncList,
      s7PlusIntervalList: ruleArray.s7PlusIntervalList,
      actionList: { 'accept': '允许', 'alert': '告警' },
      accessModeList: { 'absolute_access': '绝对访问', 'character_access': '字符访问' }
    }
  },
  mounted () {
    this.rule_map = []
    this.ruleMap.forEach(val => {
      if (val.proto === 's7_300_plus') {
        this.rule_map.push(val)
      }
    })
  },
  methods: {
    deleteRule (row) {
      this.$emit('delRuleMap', row)
    },
    editRule (row) {
      this.$emit('largeConfig', row)
    },
    protoValue (data) {
      return this.rule[data.proto]
    },
    matchOptionFormatter (data) {
      return this.$t(this.actionList[data.action])
    },
    registerAddressFormatter (data) {
      return data.register_address_min + '.' + data.register_address_min_position + '-' + data.register_address_max + '.' + data.register_address_max_position
    }
  }
}
</script>
